package com.youtui.common.util;

import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Date;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


/**
 * 短信发送
 * @author admin
 *
 */
@Component
public class DxSmsUtil {
 
	@Value("${sms.username:100244}" ) String username;
	@Value("${sms.password:asd0112}") String pwd;
	@Autowired HttpClientUtil httpUtil;
 
	
	public String sendSmsMessage_Hex(String mobile , String message) throws Exception{
		if(StringUtils.isBlank(mobile)|| StringUtils.isBlank(message)){
			return "手机号或短信内容为空";
		}
		message="【明楚科技】"+message;
		String hex = Hex.encodeHexString(message.getBytes("UTF-16BE"));
		int dateCoding =8 ;
		 
		StringBuilder sb = new StringBuilder();
		sb.append("http://61.129.57.233:7891/mt?")
		.append("un=").append(username)
		.append("&pw=").append(pwd) 
		.append("&da=").append(mobile)  
		.append("&dc=").append(dateCoding) 
		.append("&sm=").append(hex); 
		
		
		
		String result = httpUtil.doGet(sb.toString()); 
		return result; 
		 
	}
	
	/**
	 * 请求经常丢失数据无法正常发送，请勿使用
	 * @param mobile
	 * @param message
	 * @return
	 * @throws Exception
	 */
	@Deprecated
	public String sendSmsMessage_Md5(String mobile , String message) throws Exception{ 
		
		if(StringUtils.isBlank(mobile)|| StringUtils.isBlank(message)){
			return "手机号或短信内容为空";
		}
		message="【明楚科技】"+message;
		String timestamp = DateUtils.convert2String(new Date(), DateUtils.DF_LONG_COMP_STR);  
		int dateCoding =9 ;
		int transferEncoding =3;
		int responseFormat = 2;
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		md5.update(username.getBytes("utf8"));
		md5.update(pwd.getBytes("utf8"));
		md5.update(timestamp.getBytes("utf8"));
		md5.update(message.getBytes("utf8"));
		String password = Base64.encodeBase64String(md5.digest());
		
		StringBuilder sb = new StringBuilder();
		sb.append("http://61.129.57.233:7891/mt?")
		.append("un=").append(username)
		.append("&pw=").append(password)
		.append("&ts=").append(timestamp)
		.append("&da=").append(mobile)  
		.append("&dc=").append(dateCoding)
		.append("&tf=").append(transferEncoding)
		.append("&rf=").append(responseFormat)
		.append("&sm=").append(URLEncoder.encode(message,"utf8")); 
		
		
		
		String result = httpUtil.doGet(sb.toString()); 
		return result; 
		
		
	}
	 
	
}




















